#ifndef INPUTGESTURETAP_H_INCLUDED
#define INPUTGESTURETAP_H_INCLUDED

#include "InputGestureBasicFingers.h"

#include <map>

namespace tuio

{

class dittap
{
    public:
    float tstamp;
    float x,y;
};

class TeventNewTap : public TTEvent <TeventNewTap>
{
    public:
    float x,y;
};


class InputGestureTap : public CanBasicFingers< tuioApp <InputGesture> >
{
    public:
    InputGestureBasicFingers * gesturefingers;
    std::map<int,dittap *> elsdits;
    InputGestureTap()
    {
        gesturefingers = Singleton<InputGestureBasicFingers>::get();
    }

    virtual void ReceiveCall(const char * addr, osc::ReceivedMessageArgumentStream & argList)
    {
        std::list<TEvent*>::iterator it;
        for(it = gesturefingers->events.begin();
            it != gesturefingers->events.end();
            it++)
        {
            TEvent * e = *it;
            processTevent(e);
        }
    }

    //Interface redefined by ofApp
    void addTuioCursor(int32 id, float xpos,float ypos,float xspeed,float yspeed,float maccel)
    {
        dittap * d = new dittap();
        d->tstamp = ofGetElapsedTimef();
        d->x = xpos;
        d->y = ypos;
        elsdits[id] = d;
    }
    void updateTuioCursor(int32 id, float xpos,float ypos,float xspeed,float yspeed,float maccel){}
    void removeTuioCursor(int32 id)
    {
        float now = ofGetElapsedTimef();
        //std::cout<<"elapsed time"<<now<<endl;
        //std::cout<<"now - elsdits[id]->tstamp"<<now - elsdits[id]->tstamp<<endl;
        if(now - elsdits[id]->tstamp < .1)
        {
            TeventNewTap * evt = new TeventNewTap();
            evt->x = elsdits[id]->x;
            evt->y = elsdits[id]->y;
            events.push_back(evt);
        }
    }

};

template<class Base>
class CanTap : public Base
{
    public:
  ///Interficie
  virtual void newtap(double x, double y){}

  ///Handlers
  TEventHandler(TeventNewTap)
  {
      TeventNewTap * e = static_cast<TeventNewTap *>(evt);
      newtap(e->x,e->y);
  }

  ///Register
  CanTap()
  {
      TRegistraCallback(CanTap,TeventNewTap);
      registerMeToInputGestureManager(Singleton<InputGestureTap>::get());
  }

    eventprocessorsType & getEP()
    {
        return Base::getEP();
    }

};

}


#endif // INPUTGESTUREDOUBLETAP_H_INCLUDED

